Ext.define('Sofia.file.PDBReader',
{
    models: ['Pdb','Atom','Header','Remark'],
    parsePDB: function (rawtext) {

        // trim carriage returns
        rawtext = rawtext.replace(/\r/gm,"")

        // remove blank lines
        rawtext = rawtext.replace(/[\s\r\n]+$/, '');

        // generate an array of file lines
        pdbtext = rawtext.split(/\n/gm)

        var atoms = [];
        var remarks = [];
        var headers = [];
        
        // parse each line
        for (i=0; i<pdbtext.length; i++) {

            curline = pdbtext[i];

            // record is the first 6 characters
            var record = curline.substr(0,6)

            if (record.match("HEADER")) {
                //document.write("HEADER record: " + pdbtext[i] + "<br>")
            }
            else if (record.match("ATOM  ")) {
                //document.write("<p>ATOM record: <br>")

                tserial = curline.substr(6,5);
                tname = curline.substr(12,4);
                taltLoc = curline.substr(16,1);
                tresName = curline.substr(17,3);
                tchainID = curline.substr(21,1);
                tresSeq = curline.substr(22,4);
                tiCode = curline.substr(26,1);
                tx = curline.substr(30,8);
                ty = curline.substr(38,8);
                tz = curline.substr(46,8);
                toccupancy = curline.substr(54,6);
                ttempFactor = curline.substr(60,6);
                telement = curline.substr(76,2);
                tcharge = curline.substr(78,2);

                /*document.write("serial: " + tserial + "<br>")
                document.write("name: " + tname + "<br>")
                document.write("altLoc: " + taltLoc + "<br>")
                document.write("resName: " + tresName + "<br>")
                document.write("chainID: " + tchainID + "<br>")
                document.write("resSeq: " + tresSeq + "<br>")
                document.write("iCode: " + tiCode + "<br>")
                document.write("x: " + tx + "<br>")
                document.write("y: " + ty + "<br>")
                document.write("z: " + tz + "<br>")
                document.write("occupancy: " + toccupancy + "<br>")
                document.write("tempFactor: " + ttempFactor + "<br>")
                document.write("element: " + telement + "<br>")
                document.write("charge: " + tcharge + "<br>")

                document.write("</p>")*/
                
                var atom = Ext.create('Sofia.model.Atom', {
                    serial: tserial,
                    name: tname,
                    altLoc: taltLoc,
                    resName: tresName,
                    chainID: tchainID,
                    resSeq: tresSeq,
                    iCode: tiCode,
                    x: tx,
                    y: ty,
                    z: tz,
                    occupancy: toccupancy,
                    tempFactor: ttempFactor,
                    element: telement,
                    charge: tcharge
                });

                atoms = atoms.concat(atom);
            }
            else if (record.match("REMARK")) {
                //document.write("REMARK record: " + pdbtext[i] + "<br>")
            }
            else {
                //document.write("<p>ERROR: unknown record '" + record + "'<br>Content: " + pdbtext[i] + "</p>")
            }
        }
        
        var Pdb = Ext.create('Sofia.model.Pdb', {
            atoms: atoms,
            headers: headers,
            remarks: remarks
        });
        
        return Pdb;
    }    
});

